<template>
	<view class="page">
		<view class="tips">
			<text>请上传本人身份证件</text>
			<text>本系统将对信息加密，实时保障信息安全</text>
		</view>

		<view class="input">
			<input type="text" placeholder="请输入姓名" v-model="formData.name" disabled />
		</view>
		<view class="input">
			<input type="text" placeholder="请输入身份证号码" v-model="formData.identity" disabled />
		</view>
		
		<block>
			<view class="form">
				<view class="item">
					<text>人像面图片</text>
					<image :src="$wanlshop.oss(formData.renxiang)" mode="aspectFill" v-if="formData.renxiang" @tap="upload('renxiang')">
					</image>
					<image @tap="upload('renxiang')" src="/static/restaurant/common/idcard-opposite.png" mode="widthFix" v-else></image>
				</view>

				<view class="item">
					<text>国徽面图片</text>
					<image :src="$wanlshop.oss(formData.guohui)" mode="aspectFill" v-if="formData.guohui" @tap="upload('guohui')">
					</image>
					<image @tap="upload('guohui')" src="/static/restaurant/common/idcard-front.png" mode="widthFix" v-else></image>
				</view>
			</view>

			<view class="t">{{statusList[apply_status]}} <text v-if="reason">:{{reason}}</text> </view>

			<view class="btn" @tap="submit" :class="{disabled:apply_status == 2}">
				{{apply_status == 3 ? '重新' : ''}}提交
			</view>
		</block>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import graceChecker from '@/common/graceChecker';
	export default {
		data() {
			return {
				// apply_status（审核状态：1=审核通过，2=审核中，3=审核失败）
				statusList: ['', '审核已通过', '正在审核中', '审核失败',''],
				apply_status: 4,
				reason:'',
				formData: {
					name: '',
					identity: '',
					renxiang: '',
					guohui: '',
				},
				show: false,
			}
		},
		computed: {
			...mapState(['user'])
		},
		onLoad(option) {
			this.loadData(option)
		},
		methods: {
			upload(key) {
				if(this.apply_status < 3) {
					this.$wanlshop.previewImage([this.$wanlshop.oss(this.formData[key])],0)
					return false
				} 
				let token = uni.getStorageSync("token")
				let that = this
				uni.chooseImage({
					sizeType: ['original'],
					count: 1,
					async success(res){
						uni.uploadFile({
							url: `${that.$config.baseUrl}/upfile/image`,
							filePath: res.tempFilePaths[0],
							header: {token: token},
							name: 'file',
							success: (uploadFileRes) => {
								if(JSON.parse(uploadFileRes.data).code == 1) {
									that.formData[key] = JSON.parse(uploadFileRes.data).data.path;
								}
							}
						});
					}
				})
			},
			async loadData(option) {
				let res = await this.$api.getRealnameAuth();
				if (res.code == 1) {
					if(res.data) {
						this.formData = res.data;
						this.apply_status = res.data.apply_status
						this.reason = res.data.reason
					}
					if(option.name) {
						this.formData.name = option.name
						this.formData.identity = option.identity
					}
				}
			},
			async checkAge(identity) {
				let res = await this.$api.checkAge({identity:identity});
				if (res.code == 1) {
					
				}
			},
			async submit() {
				if (this.apply_status < 3) return this.$wanlshop.msg(this.statusList[this.apply_status])
				let formData = {
					name: this.formData.name,
					identity: this.formData.identity,
					renxiang: this.formData.renxiang,
					guohui: this.formData.guohui
				}
				var rule = [{
						name: 'name',
						checkType: 'notnull',
						errorMsg: '请输入姓名'
					},
					{
						name: 'identity',
						checkType: 'notnull',
						errorMsg: '请输入身份证号码'
					},
					{
						name: 'renxiang',
						checkType: 'notnull',
						errorMsg: '请上传身份证人像面图片'
					},
					{
						name: 'guohui',
						checkType: 'notnull',
						errorMsg: '请上传身份证国徽面图片'
					},
				];
				var checkRes = graceChecker.check(formData, rule);
				if (checkRes) {
					let res = await this.$api.realnameAuth(formData);
					if (res.code == 1) {
						this.$wanlshop.msg(res.msg)
						setTimeout(() => {
							this.$wanlshop.auth('/pages/restaurant/user')
						}, 1500);
					}
				} else {
					this.$wanlshop.msg(graceChecker.error);
				}
			}
		}
	}
</script>
<style lang="less">
	page {
		background-color: #fff;
	}
</style>
<style lang="less" scoped>
	.page {
		.tips {
			padding-top: 60rpx;
			margin-bottom: 66rpx;
			display: flex;
			flex-direction: column;
			align-items: center;

			text {
				&:nth-child(1) {
					color: #333333;
					font-size: 48rpx;
					font-weight: 500;
				}

				&:nth-child(2) {
					color: #999999;
					font-size: 28rpx;
					margin-top: 12rpx;
				}
			}
		}

		.t {
			padding: 30rpx;
			color: #f00;
			font-size: 28rpx;
		}

		.input {
			box-shadow: 0 0 30px 0 rgba(43, 86, 112, .1);
			border-radius: 50rpx;
			width: 690rpx;
			height: 100rpx;
			padding: 0 30rpx;
			margin: 30rpx auto;

			input {
				height: 100%;
				font-size: 30rpx;
			}
		}

		.form {
			display: flex;
			justify-content: space-around;
			align-items: center;
			margin-top: 100rpx;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;

				text {
					margin-bottom: 20rpx;
					color: #333;
					font-size: 28rpx;
				}

				image {
					width: 300rpx;
					height: 207rpx;
				}
			}
		}
	}



	.btn {
		width: 510rpx;
		height: 84rpx;
		background: #FA802A;
		border-radius: 42rpx 42rpx 42rpx 42rpx;
		margin: 200rpx auto 0;
		line-height: 84rpx;
		text-align: center;
		color: #fff;
		font-size: 36rpx;
		font-weight: 500;
		
		&.disabled {
			background-color: #ccc;
			color: #fff;
		}
	}
</style>